package com.biqee.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.biqee.dao.BqTuanDAO;
import com.biqee.dao.base.BaseDAOImpl;
import com.biqee.model.common.BqTuanStatus;
import com.biqee.model.common.PageInfoObject;
import com.biqee.model.db.BqTuan;

public class BqTuanDAOImpl extends BaseDAOImpl implements BqTuanDAO {

	@Override
	public BqTuan read(Integer id) {		
		return (BqTuan)getSqlMapClientTemplate().queryForObject(getRealSqlId("read"), id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BqTuan> listGoodTuanOfToday(int start, int count) {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("status", BqTuanStatus.GOOD.getStatus());
		condition.put("count", count);
		condition.put("start", start);
		
		return getSqlMapClientTemplate().queryForList(getRealSqlId("listTuanOfToday"), condition);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BqTuan> listCommonTuanOfToday(int start, int count) {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("status", BqTuanStatus.COMMON.getStatus());
		condition.put("count", count);
		condition.put("start", start);
		
		return getSqlMapClientTemplate().queryForList(getRealSqlId("listTuanOfToday"), condition);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BqTuan> listBadTuanOfToday(int start, int count) {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("status", BqTuanStatus.BAD.getStatus());
		condition.put("count", count);
		condition.put("start", start);
		
		return getSqlMapClientTemplate().queryForList(getRealSqlId("listTuanOfToday"), condition);
	}

	@Override
	public Integer create(BqTuan bqTuan) {
		if(bqTuan == null) {
			throw new RuntimeException("bqTuan should not be null");
		}
		
		return (Integer)getSqlMapClientTemplate().insert(getRealSqlId("create"), bqTuan);
	}

	@Override
	public Integer update(BqTuan bqTuan) {
		return (Integer)getSqlMapClientTemplate().update(getRealSqlId("updateSelective"), bqTuan);
	}

	@Override
	public BqTuan readBySiteItem(String site, String siteItemId) {
		Map<String, String> condition = new HashMap<String, String>();
		condition.put("site", site);
		condition.put("siteItemId", siteItemId);
		
		return (BqTuan)getSqlMapClientTemplate().queryForObject(getRealSqlId("readBySiteItem"), condition);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BqTuan> listTuanByPage(PageInfoObject page) {
		return getSqlMapClientTemplate().queryForList(getRealSqlId("listTuanByPage"), page);
	}

	@Override
	public Integer updateSelective(BqTuan bqTuan) {
		 return (Integer)getSqlMapClientTemplate().update(getRealSqlId("updateSelective"), bqTuan);
	}

	@Override
	public Integer delete(Integer id) {
		return  (Integer)getSqlMapClientTemplate().delete(getRealSqlId("delete"), id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BqTuan> listNullStatusRecords(int start, int number) {
		Map<String, Integer> condition = new HashMap<String, Integer>();
		condition.put("start", start);
		condition.put("number", number);
		
		return (List<BqTuan>)getSqlMapClientTemplate().queryForList(getRealSqlId("listNullStatusRecords"), condition);
	}

	@Override
	public void updateStatus(BqTuanStatus status, Integer id) {
		Map<String, Integer> condition = new HashMap<String, Integer>();
		condition.put("status", status.getStatus());
		condition.put("id", id);
		
		getSqlMapClientTemplate().update(getRealSqlId("updateStatus"), condition);
	}

	@Override
	public int countStatus(BqTuanStatus bqTuanStatus) {
		int status = bqTuanStatus.getStatus();
		return (Integer)getSqlMapClientTemplate().queryForObject(getRealSqlId("countStatus"), status);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> listAllUnExpiredJuHuaSuanItemIds() {
		String site = BqTuan.SITE_TAOBAO;
		return (List<String>)getSqlMapClientTemplate().queryForList(getRealSqlId("listAllUnExpiredJuHuaSuanItemIds"), site);
	}

	@Override
	public int countAll() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	
}